package ui.videos 
{
	import com.gaiaframework.api.Gaia;
	import com.greensock.easing.Strong;
	import com.greensock.plugins.ThrowPropsPlugin;
	import com.greensock.plugins.TweenPlugin;
	import com.greensock.TweenMax;
	import com.somerandomdude.coordy.layouts.twodee.HorizontalLine;
	import com.somerandomdude.coordy.layouts.twodee.ILayout2d;
	import com.video.VideoPlayerEvent;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;
	import pages.Pages;
	
	/**
	 * ...
	 * @author usual
	 */
	public class CustomSlider extends MovieClip 
	{
		private var _assetArray:Array;
		private var _container:MovieClip;
		private var _layout:ILayout2d;
		private var _btn:Separate;
		private var _currentSlideItem:SlideItem;
		private var _timeLine:MovieClip;
		private var _bounds:Rectangle;
		private var t1:uint, t2:uint, y1:Number, y2:Number, x1:Number, x2:Number, xOverlap:Number, xOffset:Number, yOverlap:Number, yOffset:Number;
		
		public function CustomSlider(assetArray:Array) 
		{
			_assetArray = assetArray;
			addEventListener(Event.ADDED_TO_STAGE, build);
		}
		
		public function destroy():void 
		{
			var ln:int = _assetArray.length;
			TweenMax.killTweensOf(_container);
			for (var i:int = 0; i < ln; i++) 
			{
				SlideItem(_container.getChildByName(_assetArray[i].id)).destroy();
			}
			if (_btn)
			{
				_btn.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
				_btn.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			}
			removeChild(_container);
			stage.removeEventListener(Event.RESIZE, _onResize);
			trace("[CustomSlider.destroy] ");
			if (this.parent != null) parent.removeChild(this);
		}
		
		private function build(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, build);
			_timeLine = Gaia.api.getPage(Pages.TIMELINE).content;
			TweenPlugin.activate([ThrowPropsPlugin]);
			_bounds = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);

			addElements();
			var ln:int = _assetArray.length;
			if(ln>1)addBtnForSlide();
			_onResize();
			stage.addEventListener(Event.RESIZE, _onResize);
			trace("[CustomSlider.build] ");
			checkConTainerPosition();
		}
		
		private function addBtnForSlide():void 
		{
			_btn = new Separate();
			_btn.buttonMode = true;
			_container.addChild(_btn);
			_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			
		}
		
		private function mouseDownHandler(e:MouseEvent):void 
		{
			TweenMax.killTweensOf(_container);
			x1 = x2 = _container.x;
			xOffset = this.mouseX - _container.x;
			xOverlap = Math.max(0, _container.width - _bounds.width);
			y1 = y2 = _container.y;
			yOffset = this.mouseY - _container.y;
			yOverlap = Math.max(0, _container.height - _bounds.height);
			t1 = t2 = getTimer();
			_btn.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			_btn.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			if (_currentSlideItem)
			{
				_currentSlideItem.pauseVideo();
			}
		}
		
		private function mouseMoveHandler(event:MouseEvent):void
		{
			var x:Number = this.mouseX - xOffset;
			if (x > _bounds.left) {
				_container.x = (x + _bounds.left) * 0.5;
			} else if (x < _bounds.left - xOverlap) {
				_container.x = (x + _bounds.left - xOverlap) * 0.5;
			} else {
				_container.x = x;
			}
			var t:uint = getTimer();
			if (t - t2 > 50) {
				x2 = x1;
				x1 = _container.x;
				y2 = y1;
				t2 = t1;
				y1 = _container.y;
				t1 = t;
			}
			event.updateAfterEvent();
		}
		
		private	function mouseUpHandler(event:MouseEvent):void
		{
			_btn.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			_btn.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			var time:Number = (getTimer() - t2) / 1000;
			var xVelocity:Number = (_container.x - x2) / time;
			ThrowPropsPlugin.to(_container, {
				throwProps:{
					x:{velocity:xVelocity, max:_bounds.left, min:_bounds.left - xOverlap, resistance:300}
					},
					ease:Strong.easeOut,
					onComplete:checkConTainerPosition
				},
				5,
				0.3,
				1
			);
			
		}		
		
		private function checkConTainerPosition():void 
		{
			var sw:Number = stage.stageWidth;
			var limit:Number = -(sw / 2);
			var cx:Number = _container.x
			var time:Number = .5;
			var oldCurrentItem:SlideItem = _currentSlideItem;
			if (cx == 0 || cx == -sw) time = 0;
			if (cx < limit)
			{

				_currentSlideItem = _layout.getNodeAt(1).link as SlideItem;
				TweenMax.to(_container,time,{x:-sw,ease:Strong.easeOut})
			}else
			{
				_currentSlideItem =  _layout.getNodeAt(0).link as SlideItem;
				TweenMax.to(_container, time, { x:0, ease:Strong.easeOut } );
				//Gaia.api.getPage(Pages.TIMELINE).content.updateTimeLine('out');
			}
			_timeLine.ready(_currentSlideItem);
			_currentSlideItem.unPauseVideo();
			_currentSlideItem.initVideo();
			//Gaia.api.getPage(Pages.NAV).content.footer._stageOnMouseMove()
		}
		
		private function _onResize(e:Event=null):void 
		{
			var sw:Number = stage.stageWidth;
			var sh:Number = stage.stageHeight;
			if (_container.x < 0)
			{
				_container.x = getStageWidth();
			}
			
			_layout.updateAndRender();
			if (_btn)
			{
				_btn.x = (_container.width / 2) - (_btn.width / 2);
				_btn.y = (sh / 2) - (_btn.height / 2);
			}
			_bounds.width = sw;
			_bounds.height = sh;

		}
		
		private function addElements():void 
		{
			_container = new MovieClip();
			_container.name = "_container";
			addChild(_container);
			
			_layout = new HorizontalLine(0);
			var ln:int = _assetArray.length;
			for (var i:int = 0; i < ln; i++) 
			{
				var item:SlideItem = new SlideItem(_assetArray[i]);
				item.name = _assetArray[i].id;
				_container.addChild(item);
				_layout.addNode(item);
				item.addEventListener(VideoPlayerEvent.VIDEO_READY, _videoReady);
				item.addEventListener(VideoPlayerEvent.VIDEO_COMPLETE, _videoComplete);
			}
		}
		
		private function _videoComplete(e:VideoPlayerEvent):void 
		{
			dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.VIDEO_COMPLETE));
		}
		
		private function _videoReady(e:VideoPlayerEvent):void 
		{
			
			dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.VIDEO_READY));
		}
		

		
		private function getStageWidth():Number 
		{
			var sw:Number = -_container.getChildByName(_assetArray[0].id).width
			return sw;
		}
		
		
		
	}
	
	

}